'use strict';

const axios=require("axios")
const Controller = require('egg').Controller;
const jwt=require("jsonwebtoken")

const client_id=`e71fce6b79b3d3cee30618d3bc71075ffb346b91f953e92c8f9fc98092ab5cde`,
      client_secret=`a86f13751a99850b23e52bc84375a64f9684f49d2eb21c0496ee344aeb78d62e`,
      redirect_uri=`http://localhost:7001/api/auth`
class HomeController extends Controller {


    //第三方登录
    async fromlogin() {
      const { ctx } = this;
      const {code}=ctx.query
  
      const res=await axios.post(`https://gitee.com/oauth/token?grant_type=authorization_code&code=${code}&client_id=${client_id}&redirect_uri=${redirect_uri}&client_secret=${client_secret}`)
      console.log(res)
  
      const access_token=res.data.access_token   
      
      const userInfo=await axios.get(`https://gitee.com/api/v5/user?access_token=${access_token}`)
      ctx.response.redirect('http://localhost:8080/layout/home/all')
    }



    // 登录
    async login() {
      const {
        ctx
      } = this;
      const {
        user,
        pwd
      } = ctx.request.body
      const data = await this.app.mysql.get('user', {
        user,
        pwd
      })
  console.log(data)
      if (data) {
        const token = jwt.sign({
          user,
          pwd
        }, 'zjzj', {
          expiresIn: '3d'
        })
        ctx.body = {
          code: 200,
          token,
          data,
          token1: data.type
  
        }
      } else {
        ctx.body = {
          code: 503,
          msg: '账户或密码输入不正确'
        }
      }
    }

  // 注册
  async reg() {
    const {
      ctx
    } = this;
    const {
      formState,
      data1
    } = ctx.request.body
    const {
      user,
      pwd
    } = formState
    console.log(user, pwd, data1._value)
    const data = await this.app.mysql.get('user', {
      user
    })
    if (data) {
      ctx.body = {
        code: 500,
        msg: '用户已存在'
      }
    } else {

      const res = await this.app.mysql.insert('user', {
        user: user,
        pwd: pwd,
        data: data1._value,
        type: '访客',
        pan: '可用',
        jin: '禁用',
        sou: '授权'
      })
      if (res) {
        ctx.body = {
          code: 200,
          msg: '成功'
        }
      } else {
        ctx.body = {
          code: 500,
          msg: '注册失败'
        }
      }

    }
  }

    //token 判断是否已锁定
    async one() {
      const {
        ctx
      } = this
      const {
        token
      } = ctx.request.body
      // console.log(token)
      const user = jwt.verify(token, 'zjzj')
      // console.log(user.user)
      const data = await this.app.mysql.get('user', {
        user: user.user
      })
      // console.log(data)
      if (data.pan === '可用') {
        ctx.body = {
          code: 200,
          // data
        }
      } else {
        ctx.body = {
          code: 503
        }
      }
  
    }




    // 获取路由
    async yg() {
      const { ctx } = this;
      const data=await this.app.mysql.select('list4')
      ctx.body={
        code:200,
        data:data
      }
    }
    // 获取路由相关内容
    async nr() {
      const { ctx } = this;
      const {lu}=ctx.request.body
      const data=await this.app.mysql.get('list4',{path:lu.lu})
      ctx.body={
        code:200,
        data:data
      }
    }

  // 所有文章
  async index() {
    const { ctx } = this;
    const data=await this.app.mysql.select('list')
    ctx.body={
      code:200,
      data:data
    }
  }

    // 所有文章 搜索
    async listSou() {
      const { ctx } = this;
      const {value,data2}=ctx.request.body

     await this.app.mysql.insert('sous',{name:value._value,word:data2._value,title:'1'})
      const data=await this.app.mysql.query(`select * from list where name like '%${value._value}%'`)
// console.log(name);
      ctx.body={
        code:200,
        data:data
      }
    }



//所有文章 详情
  async wenxq() {
    const { ctx } = this;
    const {id}=ctx.request.body
    console.log(id)

    const data=await this.app.mysql.get('list',{id})
    // console.log(data)
    ctx.body={
      code:200,
      data:data
    }
  }

  //所有文章 详情2
  async wenxq2() {
    const { ctx } = this;
    const {id}=ctx.request.body
    console.log(id)

    const data=await this.app.mysql.get('list',{id})
    // console.log(data)
    ctx.body={
      code:200,
      data:data
    }
  }

  //所有文章 评论
    async wenping(){
      const {ctx}=this;
      const {token,bq}=ctx.request.body
      const token1=jwt.verify(token,'zjzj')

      console.log(bq,'value')
      console.log(token1)
      const data=await this.app.mysql.insert('list2',{name:token1.user,title:bq,word:'666',tong:'未通过'})
      ctx.body={
        code:200,
        data:data
      }
    }
     //所有文章 评论 通过
async listtg(){
  const {ctx}=this;
  const data=await this.app.mysql.query(`select * from list2 where tong like '通过' `)
  ctx.body={
    code:200,
    data:data
  }
}

     //所有文章 评论 动态路由
     async listtg1(){
      const {ctx}=this;
      const data=await this.app.mysql.query(`select * from list2 where tong like '通过' `)
      ctx.body={
        code:200,
        data:data
      }
    }




      //所有文章 点赞
  async jia(){
    const {ctx}=this;
    const {id}=ctx.request.body
  
console.log(id);
 
    const data=await this.app.mysql.get('list',{id:id})
    console.log(data);
    const num=data.count+1
    await this.app.mysql.update("list",{id:id,count:num})
    ctx.body={
      code:200,
   
    }
  }

  //所有文章 类型 阅读
  async yuelist() {
    const { ctx } = this;
  

    const data=await this.app.mysql.query(`select * from list where type like '阅读'`)
    // console.log(data)
    ctx.body={
      code:200,
      data:data
    }
  }
    //所有文章 类型 前端
    async qianlist() {
      const { ctx } = this;
    
  
      const data=await this.app.mysql.query(`select * from list where type like '前端'`)
      // console.log(data)
      ctx.body={
        code:200,
        data:data
      }
    }

        //所有文章 类型 后端
        async houlist() {
          const { ctx } = this;
        
      
          const data=await this.app.mysql.query(`select * from list where type like '后端'`)
          // console.log(data)
          ctx.body={
            code:200,
            data:data
          }
        }

  
          //所有文章 标签 js
          async biaojs() {
            const { ctx } = this;
          
        
            const data=await this.app.mysql.query(`select * from list where biao like 'js'`)
            // console.log(data)
            ctx.body={
              code:200,
              data:data
            }
          }

  
          //所有文章 标签 ts
          async biaots() {
            const { ctx } = this;
          
        
            const data=await this.app.mysql.query(`select * from list where biao like 'ts'`)
            // console.log(data)
            ctx.body={
              code:200,
              data:data
            }
          }
  
          //所有文章 标签 Docker
          async biaoD() {
            const { ctx } = this;
          
        
            const data=await this.app.mysql.query(`select * from list where biao like 'Docker'`)
            // console.log(data)
            ctx.body={
              code:200,
              data:data
            }
          }
  
          //所有文章 标签 阅读
          async biaoy() {
            const { ctx } = this;
          
        
            const data=await this.app.mysql.query(`select * from list where biao like '阅读'`)
            // console.log(data)
            ctx.body={
              code:200,
              data:data
            }
          }



    // 知识小册
    async ce() {
      const { ctx } = this;
      const data=await this.app.mysql.select('list3')
      ctx.body={
        code:200,
        data:data
      }
    }

        // 知识小册 详情
        async cexq() {
          const { ctx } = this;
          const {id}=ctx.request.body
          console.log(id)

          const data=await this.app.mysql.get('list3',{id})
          ctx.body={
            code:200,
            data:data
          }
        }


// 知识小册 详情2
  async zhixq() {
    const {
      ctx
    } = this;
    const {
      id
    } = ctx.request.body
  
    const data = await this.app.mysql.get('list3',{id})

    ctx.body = {
      code: 200,
      data: data
    }

  }
}

module.exports = HomeController;
